package org.invigorare.auctions.data;


import static org.invigorare.auctions.data.SqlTypes.*;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class SaleDataOpener extends SynchronizableOutboundDataOpener {

	private static final int DATABASE_VERSION = 8;

	public static final String SALE_TABLE_NAME 	= "auction_table";
	public static final String SALE_TABLE_NULL_COL	= "nullIfNoData";	
	
	public static final String _ID				= "_id"; // Name must be exactly so, for Query Adapters to work
	
	// Metadata for this sale
	public static final String MARKET_NAME		= "market_name";
	public static final String EMPLOYEE_NO 		= "employee_No";
	public static final String ENTRY_ID 		= "entryId";
	public static final String TOLLRECEIPT_NO 	= "tollReceipt_No";
	public static final String SALE_TIMESTAMP	= "time_sold";
	public static final String DEPART_TIMESTAMP	= "time_departed";
	
	// Parties to this sale
	public static final String SELLER_LICENSE	= "seller_license_No";
	public static final String BUYER_LICENSE	= "buyer_license_No";
	
	// Content of this sale
	public static final String SALE_PRODUCE_ID 	= "sale_produceId";
	public static final String SALE_WEIGHT		= "sale_weight";
	public static final String SALE_RATE		= "sale_rate";
	public static final String SALE_TOTAL		= "sale_total";
	
	// Fees associates with this sale
	public static final String FEE_UNLOAD 		= "fee_unload";
	public static final String FEE_CLEAN		= "fee_clean";
	public static final String FEE_WEIGH		= "fee_weigh";
	
	public static final String FEE_LOAD 		= "fee_load";
	public static final String FEE_STITCH		= "fee_stitch";
	public static final String FEE_PACK			= "fee_pack";
	
	public static final String COMMISSION_AGENT	= "commission_agent";
	public static final String COMMISSION_MANDI	= "commission_mandi";
	
	private static final ColBinding[] columnBindings = new ColBinding[] {
		new ColBinding(_ID, 				INT_KEY),
		
		new ColBinding(MARKET_NAME, 		TEXT),
		new ColBinding(EMPLOYEE_NO,			TEXT),
		new ColBinding(ENTRY_ID,			TEXT),
		new ColBinding(TOLLRECEIPT_NO,		TEXT),
		new ColBinding(SALE_TIMESTAMP,		TEXT),
		new ColBinding(DEPART_TIMESTAMP,	TEXT),
		
		new ColBinding(SELLER_LICENSE,		TEXT),
		new ColBinding(BUYER_LICENSE,		TEXT),
		
		new ColBinding(SALE_PRODUCE_ID,		INT),
		new ColBinding(SALE_WEIGHT,			REAL),
		new ColBinding(SALE_RATE,			REAL),
		new ColBinding(SALE_TOTAL,			REAL),
		
		new ColBinding(FEE_UNLOAD,			REAL),
		new ColBinding(FEE_CLEAN,			REAL),
		new ColBinding(FEE_WEIGH,			REAL),
		new ColBinding(FEE_LOAD,			REAL),
		new ColBinding(FEE_STITCH,			REAL),
		new ColBinding(FEE_PACK,			REAL),
		
		new ColBinding(COMMISSION_AGENT,	REAL),
		new ColBinding(COMMISSION_MANDI,	REAL)
	};
	
	public SaleDataOpener(Context context) {
		super(context, SALE_TABLE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(createTableString(SALE_TABLE_NAME, columnBindings));
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
		db.execSQL("DROP TABLE " + SALE_TABLE_NAME);
		db.execSQL(createTableString(SALE_TABLE_NAME, columnBindings));
	}
	
	public static SQLiteDatabase getWritableDb(Context context) {
		SaleDataOpener dbHelper = new SaleDataOpener(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		return db;
	}
	
	public static SQLiteDatabase getReadableDb(Context context) {
		SaleDataOpener dbHelper = new SaleDataOpener(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		return db;
	}

}
